package sf.tools;

public class JavaTypeUtils {
    /**
     * 判断是否是数字类型
     * @param clz
     * @return
     */
    public static boolean isNumberClass(Class<?> clz) {
        if (Number.class.isAssignableFrom(clz) || clz == byte.class || clz == short.class || clz == int.class || clz == long.class
                || clz == float.class || clz == double.class) {
            return true;
        }
        return false;
    }

    /**
     * 是否是整数
     * @param type
     * @return
     */
    public static boolean isInt(Class type) {
        if (type == byte.class || type == int.class || type == long.class || type == short.class
                || type == Byte.class || type == Short.class || type == Integer.class || type == Long.class) {
            return true;
        }
        return false;
    }

    public static boolean isLong(Class type) {
        if (type == long.class || type == Long.class) {
            return true;
        }
        return false;
    }

    /**
     * 原始类型转换为包装类型
     * @param clz
     * @return
     */
    public static Class<?> changePrimitiveToWrapperType(Class<?> clz) {
        if (clz.isPrimitive()) {
            if (clz == Integer.TYPE) {
                clz = Integer.class;
            } else if (clz == Boolean.TYPE) {
                clz = Boolean.class;
            } else if (clz == Long.TYPE) {
                clz = Long.class;
            } else if (clz == Byte.TYPE) {
                clz = Byte.class;
            } else if (clz == Short.TYPE) {
                clz = Short.class;
            } else if (clz == Float.TYPE) {
                clz = Float.class;
            } else if (clz == Double.TYPE) {
                clz = Double.class;
            } else if (clz == Character.TYPE) {
                clz = Character.class;
            } else if (clz == Void.TYPE) {
                clz = Void.class;
            }
        }
        return clz;
    }

    /**
     * 获取基本类型的默认值
     * @param clz
     * @return
     */
    public static Object getClassPrimitiveDefaultValue(Class<?> clz) {
        Object obj = null;
        if (clz.isPrimitive()) {
            if (clz == Integer.TYPE) {
                obj = 0;
            } else if (clz == Boolean.TYPE) {
                obj = false;
            } else if (clz == Long.TYPE) {
                obj = 0L;
            } else if (clz == Byte.TYPE) {
                obj = 0;
            } else if (clz == Short.TYPE) {
                obj = 0;
            } else if (clz == Float.TYPE) {
                obj = 0F;
            } else if (clz == Double.TYPE) {
                obj = 0D;
            } else if (clz == Character.TYPE) {
                obj = (char) 0;
            }
        }
        return obj;
    }

    /**
     * @param obj         值
     * @param targetClass 目标类型
     * @return
     */
    public Object convertType(Object obj, Class<?> targetClass) {
        if (obj == null || obj.getClass() == targetClass) {
            return obj;
        }
        if (obj.getClass().isPrimitive() && targetClass.isPrimitive()) {
            if (targetClass == Boolean.TYPE || targetClass == Boolean.class) {
                obj = false;
            } else if (targetClass == Byte.TYPE || targetClass == Byte.class) {
                obj = 0;
            } else if (targetClass == Short.TYPE || targetClass == Short.class) {
                obj = 0;
            } else if (targetClass == Integer.TYPE || targetClass == Integer.class) {
                obj = 0;
            } else if (targetClass == Long.TYPE || targetClass == Long.class) {
                obj = 0L;
            } else if (targetClass == Float.TYPE || targetClass == Float.class) {
                obj = 0F;
            } else if (targetClass == Double.TYPE || targetClass == Double.class) {
                obj = 0D;
            } else if (targetClass == Character.TYPE || targetClass == Character.class) {
                obj = (char) 0;
            }
        }
        return 0;
    }

}
